#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<stdio.h>
using namespace std;



void usage_windows();
void usage_linux(char* argv[]);
bool compute(double iTotSum, double iRefX, double iRefY, double iRefZ,bool bPrint=false);


int main(int args,char* argv[])
{
	
#ifndef LINUX
	usage_windows();
#else
	usage_linux(argv);
#endif
	return 0;
}

bool compute(double dTotSum, double dRefX, double dRefY, double dRefZ, bool bPrint)
{

	/*************define loop times***********/  
	int cnt_x=int(dTotSum/dRefX);
	int cnt_y=int(dTotSum/dRefY);
	int cnt_z=int(dTotSum/dRefZ);

	if(int(dRefX)==0)
	{
		cnt_x=1;
	}
	if(int(dRefY)==0)
	{
		cnt_y=1;
	}
	if(int(dRefZ)==0)
	{
		cnt_z =1;
	}
	/*************define loop times***********/ 
	
	/*****************************************/ 
	//must double
//	double dNowVal; //dNowVal = x*dRefX+y*dRefY+z*dRefZ;  
//	double dTmpMax=dTotSum/2;  //iNowVal>iTmpMax
	double dTmpMax=(dRefX+dRefY+dRefZ);
	cout<<"dTmpMaxInit="<<dTmpMax<<endl;
	/*****************************************/ 

	/***************init variables*************/ 
	int x=1;
	int y=1;
	int z=1;

	int dest_x=0;         // when iNowVal is the biggest
	int dest_y=0;         //
	int dest_z=0;         //

	double dNowVal = x*dRefX+y*dRefY+z*dRefZ;//dNowVal = x*dRefX+y*dRefY+z*dRefZ;  
	/******************************************/ 
	if(bPrint){
		cout<<"cnt_x="<<cnt_x<<endl;
		cout<<"cnt_y="<<cnt_y<<endl;
		cout<<"cnt_z="<<cnt_z<<endl;
		cout<<"Total_SUM="<<dTotSum<<endl;
		cout<<"coef_x="<<dRefX<<endl;
		cout<<"coef_y="<<dRefY<<endl;
		cout<<"coef_z="<<dRefZ<<endl;
		cout<<"--------------------------------"<<endl;
	}
	for(;x<=cnt_x;x++){
		y=1;
		for(;y<=cnt_y;y++){	
			z=1;
			for(;z<=cnt_z;z++){
				dNowVal = x*dRefX+y*dRefY+z*dRefZ;
				if((dNowVal>=dTmpMax)&&(dNowVal<=dTotSum)){
					dTmpMax=dNowVal;
					dest_x=x;
					dest_y=y;
					dest_z=z;
//					cout<<"dNowVal="<<dNowVal<<endl;
				}
				if(bPrint){
					cout<<"x="<<x<<endl;
					cout<<"y="<<y<<endl;
					cout<<"z="<<z<<endl;
					cout<<"dNowVal="<<dNowVal<<endl;
					cout<<"----------------"<<endl;	
				}
			}
		}
	}
	if((dest_x==0)&&(dest_y==0)&&(dest_z==0))
	{
		cout<<"------Not Found The Answer... \n"<<"--------CHECK YOUR coefficient AND TotSum YOU SET------"<<endl;
		
	}
	else{
		if(int(dRefX)==0){
			dest_x=0;
		}
		if(int(dRefY)==0){
			dest_y=0;
		}
		if(int(dRefZ)==0){
			dest_z =0;
		}
		cout<<"The Max is: "<<dTmpMax<<endl;
		cout<<"The left is: "<<(dTotSum-dTmpMax)<<endl;
		cout<<"less than "<<dTotSum<<endl;
		cout<<"x: "<<dest_x<<endl;
		cout<<"y: "<<dest_y<<endl;
		cout<<"z: "<<dest_z<<endl;
	}

	return 0;
}

void usage_windows()
{
	double dTotSum=0;
	double dRefX=0;
	double dRefY=0;
	double dRefZ=0;
	char cKlik='N';  //Default, not to print all the process, just the result.

	while(true){
		cout<<"\n\n\n-------Input values-----------\n";
		cout<<"-------coefficientX:>";
		cin>>dRefX;
		cout<<"-------coefficientY:>";
		cin>>dRefY;
		cout<<"-------coefficientZ:>";
		cin>>dRefZ;
		cout<<"-------TotalSum:>";
		cin>>dTotSum;
		//check
		if(dTotSum<dRefX||dTotSum<dRefY||dTotSum<dRefZ)
		{
			cout<<"---------Check your sytax---------\n"
				<<"---------should input four data--------"<<endl;
		}
		// print the result or all the process
		cout<<"--------JUST Print the result ? (Y/N)"<<endl;
		cin>>cKlik;
		cout<<"--------------------------------"<<endl;
		if(cKlik=='Y'||cKlik=='y'||cKlik==NULL)
		{
			compute(dTotSum,dRefX,dRefY,dRefZ);
		}
		else if(cKlik=='N'||cKlik=='n'){
			compute(dTotSum,dRefX,dRefY,dRefZ,1);
		}
		else
		{
			cout<<"----------Check your sytax--------------"<<endl;
		}
		
	}
}



void usage_linux(char* argv[])
{
	double dTotSum=0;
	double dRefX=0;
	double dRefY=0;
	double dRefZ=0;
	bool bPrint = false;

	dRefX = atof(argv[1]);
	dRefY = atof(argv[2]);
	dRefZ = atof(argv[3]);
	dTotSum = atof(argv[4]);
	bPrint =bool( atoi(argv[5]));

//	dRefX = sprintf(dRefX,);(argv[1]);
	if(dTotSum<dRefX||dTotSum<dRefY||dTotSum<dRefZ)
	{
		cout<<"---------Check your sytax---------\n"
			<<"---------should input four data--------"<<endl;
	}
	compute(dTotSum,dRefX,dRefY,dRefZ,bPrint);

}
